package com.iot.system.core.impl;

import com.iot.common.core.domain.R;
import com.iot.common.utils.DateUtils;
import com.iot.common.utils.bean.BeanUtils;
import com.iot.system.core.CoreDeviceStateLogsService;
import com.iot.system.domain.DeviceDetails;
import com.iot.system.domain.DeviceStateLogs;
import com.iot.system.domain.vo.DeviceStateLogsVo;
import com.iot.system.service.IDeviceDetailsService;
import com.iot.system.service.IDeviceStateLogsService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CoreDeviceStateLogsServiceImpl implements CoreDeviceStateLogsService {


    @Resource
    private IDeviceStateLogsService iDeviceStateLogsService;

    @Resource
    private IDeviceDetailsService iDeviceDetailsService;

    @Override
    public List<DeviceStateLogsVo> selectDeviceStateLogsList(DeviceStateLogs deviceStateLogs) {
        List<DeviceStateLogs> stateLogsList = iDeviceStateLogsService.selectDeviceStateLogsList(deviceStateLogs);
        if (CollectionUtils.isEmpty(stateLogsList)){
            return new ArrayList<>();
        }

        Map<Long, DeviceDetails> detailsMap = getLongDeviceDetailsMap(stateLogsList);

        List<DeviceStateLogsVo> deviceStateLogsVos = stateLogsList.stream().map(item -> {
            DeviceStateLogsVo deviceStateLogsVo = new DeviceStateLogsVo();
            BeanUtils.copyProperties(item, deviceStateLogsVo);
            if (detailsMap.containsKey(item.getDeviceId())) {
                deviceStateLogsVo.setDeviceName(detailsMap.get(item.getDeviceId()).getDeviceName());
                deviceStateLogsVo.setDeviceTemsCode(detailsMap.get(item.getDeviceId()).getDeviceTemsCode());
                deviceStateLogsVo.setBootUpDate(DateUtils.getDateFormatByDate(item.getDeviceBootUp()));
                deviceStateLogsVo.setShutDownDate(DateUtils.getDateFormatByDate(item.getDeviceShutDown()));
            }
            return deviceStateLogsVo;
        }).collect(Collectors.toList());
        return deviceStateLogsVos;
    }

    private Map<Long, DeviceDetails> getLongDeviceDetailsMap(List<DeviceStateLogs> stateLogsList) {
        List<Long> deviceIds = stateLogsList.stream().map(DeviceStateLogs::getDeviceId).collect(Collectors.toList());
        List<DeviceDetails> deviceDetails = iDeviceDetailsService.selectDeviceNameAndTemsCodeByIds(deviceIds);
        Map<Long, DeviceDetails> detailsMap = deviceDetails.stream().collect(Collectors.toMap(item -> item.getId(), item -> item));
        return detailsMap;
    }

    @Override
    public DeviceStateLogs selectDeviceStateLogsById(Long id) {
        return iDeviceStateLogsService.selectDeviceStateLogsById(id);
    }

    @Override
    public int insertDeviceStateLogs(DeviceStateLogs deviceStateLogs) {
        return iDeviceStateLogsService.insertDeviceStateLogs(deviceStateLogs);
    }

    @Override
    public int updateDeviceStateLogs(DeviceStateLogs deviceStateLogs) {
        return iDeviceStateLogsService.updateDeviceStateLogs(deviceStateLogs);
    }

    @Override
    public int deleteDeviceStateLogsByIds(Long[] ids) {
        return iDeviceStateLogsService.deleteDeviceStateLogsByIds(ids);
    }

    @Override
    public Integer getDeviceRunTime() {

        return 1000;
    }
}
